<template>
  <div class="course-info">
    <van-cell-group :style="styleOption">
      <van-cell class="course-image">
        <img :src="course.courseImgUrl">
      </van-cell>
      <van-cell class="course-description">
        <h2 v-text="course.courseName"></h2>
        <p v-text="course.brief"></p>
        <div class="course-sale-info">
          <p class="course-price">
            <span class="discounts">¥{{course.discounts}}</span>
            <span>¥{{course.price}}</span>
          </p>
          <span class="tag">{{course.sales}}人已经购买</span>
          <span class="tag">周三周五更新</span>
        </div>
      </van-cell>
      <van-cell class="course-detail">
        <van-tabs sticky scrollspy>
          <van-tab title="详情">
            <p v-html="course.courseDescription"></p>
          </van-tab>
          <van-tab title="内容">
            <course-section-and-lesson v-for="item in sections.courseSectionList" :key="item.id" :sectionData="item"></course-section-and-lesson>
          </van-tab>
        </van-tabs>
      </van-cell>
    </van-cell-group>
    <van-tabbar v-if="!course.isBuy">
      <div class="price">
        <span v-text="course.discountsTag"></span>
        <span class="discounts">¥{{course.discounts}}</span>
        <span>¥{{course.price}}</span>
      </div>
      <van-button type="primary" @click="handlePayAction">立即购买</van-button>
    </van-tabbar>
  </div>
</template>

<script>
import { Tabbar, CellGroup, Tabs, Tab, Cell, Button } from 'vant'
import { getCourseById, getSectionAndLesson } from '@/services/course'
import CourseSectionAndLesson from './components/CourseSectionAndLesson.vue'
export default {
  components: { CourseSectionAndLesson, VanTabbar: Tabbar, VanCellGroup: CellGroup, VanTabs: Tabs, VanTab: Tab, VanCell: Cell, VanButton: Button },
  name: 'CourseInfo',
  data () {
    return {
      course: {},
      sections: {},
      styleOption: {}
    }
  },
  props: {
    courseId: {
      type: [String, Number],
      required: true
    }
  },
  created () {
    this.loadCourseInfo()
    this.loadSections()
  },
  methods: {
    handlePayAction () {
      console.log('点击购买')
      if (this.$store.state.user) {
        this.$router.push({ name: 'pay', params: { courseId: this.courseId } })
      } else {
        this.$router.push({ name: 'Login', query: { directe: this.$route.fullPath } })
      }
    },
    async loadSections () {
      const { data } = await getSectionAndLesson({ courseId: this.courseId })
      if (data.state === 1) {
        this.sections = data.content
      }
    },
    async loadCourseInfo () {
      const { data } = await getCourseById({ courseId: this.courseId })
      if (data.state === 1) {
        this.course = data.content
        if (data.content.isBuy) {
          this.styleOption.bottom = 0
        } else {
          this.styleOption.bottom = 50
        }
      }
    }
  }
}
</script>

<style scoped>
.van-cell {
  padding: 0;
}
.course-image {
  height: 280px;
}
.course-image img {
  width: 100%;
}

.course-description {
  padding: 10px 20px;
  height: 150px;
}
.course-sale-info {
  display: flex;
}
.course-sale-info .course-price {
  flex: 1;
  margin: 0;
}
.discounts {
  color: red;
  font-size: 24px;
  font-weight: 700;
  margin-right: 5px;
}
.tag {
  background-color: #f8f9fa;
  font-size: 12px;
  font-weight: 700;
  margin-left: 10px;
  padding: 7px;
  line-height: 15px;
  border-radius: 7px;
}
.van-tabbar {
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
  box-sizing: border-box;
  line-height: 50px;
  align-items: center;
}
span {
  font-size: 12px;
}
.van-button {
  width: 50%;
  height: 40px;
}
.van-cell-group {
  position: fixed;
  top: 0;
  bottom: 50px;
  width: 100%;
  overflow-y: auto;
}
</style>
